package com.zx.mes.hyl.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.mes.controller.BaseController;
import com.zx.mes.hyl.entity.*;
import com.zx.mes.hyl.pagemodel.TreeNode;
import com.zx.mes.hyl.service.SysMenuService;
import com.zx.mes.hyl.service.SysRoleMenuService;
import com.zx.mes.response.ObjectRestResponse;
import com.zx.mes.response.TableResultResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华云龙
 * @since 2018-07-09
 */
@CrossOrigin
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController<SysMenuService,SysMenu>{

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public ObjectRestResponse<SysMenu> add(SysMenu sysMenu) {

        sysMenu.setId(UUID.randomUUID().toString());
        return super.add(sysMenu);
    }

    @Override
    public TableResultResponse<SysMenu> list(@RequestParam Map<String, Object> params, SysMenu sysMenu) {
        int page = Integer.valueOf(params.get("page").toString());
        int limit = Integer.valueOf(params.get("limit").toString());

        Wrapper<SysMenu> wrapper = new EntityWrapper<>();
        if (sysMenu != null && !"".equals(sysMenu.getName())) {
            wrapper.like("name", sysMenu.getName());
        }

        List<SysMenu> list = this.biz.selectPage(new Page(page, limit),wrapper).getRecords();
        TableResultResponse<SysMenu > response = null;
        if (list == null) {
            response = new TableResultResponse("无数据", 200, 0L, (long)page, (List)null);
        } else {
            response = new TableResultResponse("分页查询结果", 200, (long)this.biz.selectCount(new EntityWrapper()), (long)page, list);
        }

        return response;
    }

    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/deleteBatch")
    public ObjectRestResponse<String> deleteBatch(String ids) {
        ObjectRestResponse<String> response = new ObjectRestResponse<>();
        try {
            /// 删除sysMenu表中的数据
            biz.deleteBatchIds(Arrays.asList(ids.split(",")));
            /// 删除sysRoleMenu表中对应的数据
            sysRoleMenuService.delete(new EntityWrapper<SysRoleMenu>()
                    .in("menu_id",ids));
        } catch (Exception e) {
            response.setMessage("菜单批量删除:"+e.getMessage());
            response.setStatus(500);
            e.printStackTrace();
        }
        response.setMessage("批量删除成功!");
        return response;
    }

    @GetMapping("/getAllTreeNode")
    public ObjectRestResponse<SysUser> getAllTreeNode(){
        ObjectRestResponse<SysUser> response = new ObjectRestResponse<>();
        /// 从后台获取所有的菜单权限
        List<SysMenu> menuList = biz.selectList(new EntityWrapper<>());
        List<TreeNode> nodeList = new ArrayList<>(menuList.size()+1);
        List<TreeNode> list = new ArrayList<>();
        /// list集合 由SysMenu ---> TreeNode
        for (int i = 0; i <menuList.size() ; i++) {
            SysMenu sysMenu = menuList.get(i);
            TreeNode treeNode = setTreeNode(sysMenu);
            nodeList.add(treeNode);
        }
        /// tree 生成
        for (int i = 0; i < nodeList.size() ; i++) {
            TreeNode node = nodeList.get(i);
            /// 判断顶级级点
            if (!StringUtils.isNotBlank(node.getPid())) {
                list.add(getTree(node, nodeList));
            }
        }
        SysUser sysUser = new SysUser();
        sysUser.setTreeNodeList(list);
        response.setData(sysUser);
        response.setMessage("权限树获取成功!");
        return response;
    }

    private TreeNode setTreeNode(SysMenu sysMenu) {
        TreeNode treeNode = new TreeNode();
        treeNode.setTitle(sysMenu.getName());
        treeNode.setKey(sysMenu.getId());
        treeNode.setPid(sysMenu.getPid());
        treeNode.setId(sysMenu.getId());
        return treeNode;
    }

    private TreeNode getTree(TreeNode node, List<TreeNode> nodeList) {
        for (int i = 0; i <nodeList.size() ; i++) {
            TreeNode treeNode = nodeList.get(i);
            if (StringUtils.isNotBlank(treeNode.getPid()) && treeNode.getPid().equals(node.getId())) {
                node.getChildren().add(treeNode);
                getTree(treeNode, nodeList);
            }
        }
        return node;
    }
}

